package com.hanqi.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hanqi.entity.SysFuncs;
import com.hanqi.service.SysFuncsService;
import com.hanqi.util.PinyinUtil;
import com.hanqi.util.R;
import com.hanqi.util.UUIDGenerator;
import com.hanqi.util.shiro.ShiroUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author zjk
 * @since 2019-04-27
 */
@Controller
@RequestMapping("/sysFuncs")
public class SysFuncsController {

    @Autowired
    protected SysFuncsService sysFuncsService;

    @RequestMapping("/index")
    public String index() {
        return "page/funcList";
    }

    @ResponseBody
    @RequestMapping("/list")
    public List<SysFuncs> list() {
        QueryWrapper<SysFuncs> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("delete_flag", 0);
        return sysFuncsService.list(queryWrapper);
    }

    @ResponseBody
    @RequestMapping("/insert")
    public JSONObject insert(SysFuncs sysFuncs) {
        sysFuncs.setId(UUIDGenerator.getUUID());
        String funcName = sysFuncs.getFuncName();
        sysFuncs.setPermissions(PinyinUtil.getPingYin(funcName));
        sysFuncs.setCreateDate(LocalDateTime.now());
        sysFuncs.setCreateName(ShiroUtil.getUserEntity().getRname());
        sysFuncs.setDeleteFlag(0);
        boolean r = sysFuncsService.save(sysFuncs);
        if (r) {
            return R.SUCCESS;
        }
        return R.FAIL;
    }

    @ResponseBody
    @RequestMapping("/update")
    public JSONObject update(SysFuncs sysFuncs) {
        boolean r = sysFuncsService.updateById(sysFuncs);
        if (r) {
            return R.SUCCESS;
        }
        return R.FAIL;
    }

    @ResponseBody
    @RequestMapping("/delete")
    public JSONObject delete(String ids) {
        String[] idCollect = ids.split(",");
        boolean r = sysFuncsService.removeByIds(Arrays.asList(idCollect));
        if (r) {
            return R.SUCCESS;
        }
        return R.FAIL;
    }

    @ResponseBody
    @RequestMapping("/allBtns")
    public List<SysFuncs> allBtns(String menuId) {
        return sysFuncsService.allBtns(menuId);
    }

    @ResponseBody
    @RequestMapping("/currentBtns")
    public List<SysFuncs> currentBtns(String menuId) {
        return sysFuncsService.currentBtns(menuId);
    }
}
